package com.yulongtian.oneProblemEveryDay.month202301;

/**
 * https://leetcode.cn/problems/ways-to-make-a-fair-array/
 * 可
 *
 * @author yulongTian
 * @create 2023-01-28 15:37
 */
public class Test19 {
    public static void main(String[] args) {
        System.out.println(waysToMakeFair(new int[]{1, 2, 3, 2, 1}));
    }

    public static int waysToMakeFair(int[] nums) {
        int ans = 0;

        //前奇数和
        int preJi = 0;
        //前偶数和
        int preOu = 0;
        //后奇数和
        int latterJi = 0;
        //后偶数和
        int latterOu = 0;

        for (int i = 0; i < nums.length; i++) {
            if ((i & 1) == 1) {
                latterJi += nums[i];
            } else {
                latterOu += nums[i];
            }
        }
        for (int i = 0; i < nums.length; i++) {
            //删除索引i
            if (i == 0) {
                //只要看latter
                if (latterOu - nums[0] == latterJi) {
                    ans++;
                }
                latterOu -= nums[0];
                //互换
                int temp = latterJi;
                latterJi = latterOu;
                latterOu = temp;
            } else {
                //i-1为奇数索引  i为偶数
                if (((i - 1) & 1) == 1) {
                    //前面加上
                    preJi += nums[i - 1];
                    //后面删除
                    latterJi -= nums[i];
                } else {
                    preOu += nums[i - 1];
                    latterOu -= nums[i];
                }

                //比较
                if (preJi + latterJi == preOu + latterOu) {
                    ans++;
                }
            }
        }
        return ans;
    }

}
